<?
require_once('unicode.php');
$FILE = 'unicode.counts';

if(count($argv) < 2) {
  printf("php %s (utf8 file)\n",$argv[0]);
  return;
}

if(file_exists($FILE)) {
  $counts = unserialize(file_get_contents($FILE));
}
else $counts = array();

if($argv[1] === 'print') {
  print_r($counts);

}
else {
  $fp = fopen($argv[1],'rb');
  if(!$fp) return;

  while(!feof($fp)) {
    $ch = fgetc($fp);
    if($ch === false) break;
    $str = $ch;
    $len = utf8len($str);
    while(strlen($str) < $len) {
      $ch = fgetc($fp);
      if($ch === false) die("Incomplete utf8 character.\n");
      $str .= $ch;
    }
    if(key_exists($str,$counts)) $counts[$str]++;
    else $counts[$str] = 1;
  }
  fclose($fp);

  file_put_contents($FILE,serialize($counts));
}

?>
